<template>
	<view>
		<view class="item">
			<view class="">
				头像
			</view>
			<view @click="changePhoto" class="">
				<image :src="photo(userInfo.photo)"></image>
			</view>
		</view>
		<view class="item">
			<view class="">
				昵称
			</view>
			<view class="">
				{{userInfo.name}}
			</view>
		</view>
		<view class="item">
			<view class="">
				性别
			</view>
			<view class="">
				{{userInfo.sex ==1 ? '男' : userInfo.sex==0 ? '女' :'未知'}}
			</view>
		</view>
		<button @click="submit" type="primary">保存</button>
	</view>
</template>

<script>
	import {
		chooseImage,
		uploadFile
	} from '../../utils/images.js'
	import {
		update
	} from '../../service/member.js'
	import {
		BASE_IMG_URL,
		BASE_URL
	} from "../../utils/config.js"
	export default {
		data() {
			return {
				baseImg: BASE_IMG_URL,
				userInfo: {
					name: "",
					photo: "",
					sex: null
				}
			}
		},
		onLoad() {

			if (uni.getStorageSync("member")) {
				this.userInfo = uni.getStorageSync("member");


			}
		},

		methods: {
			async submit() {

				const {
					errCode
				} = await update(this.userInfo);
				if (errCode === 10000) {
					uni.showToast({
						title: '修改成功'
					})

					uni.setStorageSync("member", this.userInfo);
				}

			},
			photo(val) {
				if (val) {
					return this.baseImg + val;
				}
				return '../../static/images/photo.png';
			},
			async changePhoto() {

				const res = await chooseImage(1).catch(() => {});
				if (!res) {
					uni.showToast({
						title: '请选择图片',
						icon: 'none'
					})
					return;
				}
				const {errCode,data} =await uploadFile(res[0]).catch(() => {});
				if (errCode !== 10000) {
					uni.showToast({
						title: '上传失败',
						icon: 'none'
					})
					return ;
				} 
				this.userInfo.photo = data;
					
				 

				// uni.uploadFile({
				// 			url: BASE_URL+'upload/add', //仅为示例，非真实的接口地址
				// 			filePath: res[0],
				// 			name: 'file',
				// 			header:{'Authorization':uni.getStorageSync("token")},
				// 			success: (uploadFileRes) => {
				// 				console.log(uploadFileRes.data);
				// 				const {errCode,data}=JSON.parse(uploadFileRes.data);
				// 				if(errCode===10000){
				// 					this.userInfo.photo=data;
				// 				}
				// 				else{
				// 					uni.showToast({
				// 						title:'上传失败',icon:'none'
				// 					})
				// 				}

				// 			}
				// 		});


				// uni.chooseImage({
				// 	count: 1, //默认9
				// 	sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
				// 	sourceType: ['album','camera'], //从相册选择
				// 	success: function (res) {
				// 		console.log(res.tempFilePaths[0]);


				// 	}
				// });


			}
		}
	}
</script>

<style lang="less">
	.item {
		display: flex;
		justify-content: space-between;
		border-bottom: solid 1rpx #dddddd;
		align-items: center;
		min-height: 80rpx;
		padding: 0 20rpx;

		image {
			width: 75rpx;
			height: 75rpx;
			border-radius: 50%;
		}
	}
</style>